package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.*;
import com.situ.springboot.service.CommentService;
import com.situ.springboot.service.ServiceService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {

    //功能: 普通管理员可以删除评论, 客户可以发表评论(点添加按钮)

    @Autowired
    private CommentService commentService;

    @Autowired
    private ServiceService serviceService;

    //管理员评论展示
    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("CommentController.selectByPage");
        PageInfo pageInfo = commentService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到comment_list界面展示
        return "/comment/list";
    }

    //客户评论展示
    @RequestMapping("/selectByPageCustom")
    public String selectByPageCustom(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("CommentController.selectByPage");
        PageInfo pageInfo = commentService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到comment_list界面展示
        return "/comment/custom_list";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        commentService.deleteById(id);
        //redirect:重定向，告诉浏览器发送请求 /blog/selectAll
        //删除完之后，应该查找展示最新的数据
        return "redirect:/comment/selectByPage";
        //return "/blog/selectAll";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        commentService.deleteAll(ids);
        return "redirect:/comment/selectByPage";
    }


    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<Service> list = serviceService.selectAll();
        model.addAttribute("list", list);
        return "/comment/add";
    }

    @RequestMapping("/add")
    public String add(Comment comment, HttpSession session) {

        Custom custom = (Custom) session.getAttribute("custom");
        Integer customId = custom.getId();

        System.out.println("这是服务的id" + comment.getServiceId());

        commentService.add(comment, customId);
        return "redirect:/comment/selectByPageCustom";
    }
}
